package nets

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"time"

	"github.com/k0kubun/pp"

	"cli/config"
)

func BuyCardByName(endDate time.Time, times, inviteTeacherId, price int, userName, cardName string) {
	targetUrl := config.Config.CliInfo.Url + "/api/root/insert/purchase/card/by/name"
	dataMap := make(map[string]interface{})
	dataMap["card_name"] = cardName
	dataMap["sudoAuthentication"] = AuthenticationInfo
	dataMap["user_name"] = userName
	dataMap["money"] = price
	dataMap["end_date"] = endDate
	dataMap["times"] = times
	dataMap["invite_teacher_id"] = inviteTeacherId
	payload, _ := json.Marshal(dataMap)
	req, _ := http.NewRequest("POST", targetUrl, bytes.NewReader(payload))
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	resqData, _ := io.ReadAll(response.Body)
	if response.StatusCode != 200 {
		fmt.Println("Error:", string(resqData))
	} else {
		fmt.Println(string(resqData))
	}
}
func SelectAllCardInfo() {
	targetUrl := config.Config.CliInfo.Url + "/api/select/all/card"
	req, _ := http.NewRequest("POST", targetUrl, nil)
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	type ResponseData struct {
		Cards []CardComplexInfo `json:"cards"` // Cards 是一个数组
	}

	var responseData ResponseData // 定义一个接收响应数据的结构体

	resqData, _ := io.ReadAll(response.Body)
	if response.StatusCode != 200 {
		fmt.Println("Error:", string(resqData))
	} else {
		fmt.Println("得到请求到的会员卡信息如下")
		err = json.Unmarshal(resqData, &responseData) // 解析为 ResponseData 结构体
		if err != nil {
			fmt.Println("Error unmarshaling JSON:", err)
			return
		}
		// 打印解析后的 cards 数据
		pp.Print(responseData.Cards)
	}
}
